package mn.idax.exchange.otc.exception;

import com.alibaba.fastjson.JSON;
import mn.idax.exchange.common.I18nLanguageUtil;
import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.dto.ResponseDto;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResponseData handle(Exception e) {
        logger.error("----otc-chat global exception:{}----", ExceptionUtils.getStackTrace(e));
        return ResponseData.error(ResponseDto.system_error_code, ResponseDto.system_error_msg);
    }
    /**
     *
     * 功能描述: 自定义异常类捕捉
     * @param: e 自定义异常类型
     * @return:  ResponseData 公共实体响应内容
     */
    @ExceptionHandler(value = IdaxException.class)
    @ResponseBody
    public ResponseData handleIdaxException(IdaxException e){
        // 国际化
        return (ResponseData) I18nLanguageUtil.replaceLangeage(JSON.toJSONString(ResponseData.error(e.getCode(),null)));
    }
}
